package com.tongji.hbase.migration.util;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * The default logger.
 * 
 * @author Zhao Long
 */
public class DefaultLogger implements Logger {

	private static final SimpleDateFormat SDF = new SimpleDateFormat(
			"yy/MM/dd HH:mm:ss");

	private String className;

	/**
	 * Constructor.
	 */
	public DefaultLogger() {
		
	}

	@Override
	public void init(Class<?> clazz) {
		className = clazz.getSimpleName();
	}
	
	@Override
	public void setLevel(int aLevel) {
		
	}
	
	@Override
	public void info(String aMessage) {
		printOut("INFO", aMessage);
	}

	@Override
	public void warn(String aMessage) {
		printOut("WARN", aMessage);
	}

	@Override
	public void warn(String aMessage, Throwable aThrowable) {
		warn(aMessage + " exception=" + aThrowable);
		aThrowable.printStackTrace();
	}

	@Override
	public void error(String aMessage) {
		printErr("ERROR", aMessage);
	}

	@Override
	public void error(String aMessage, Throwable aThrowable) {
		error(aMessage + " exception=" + aThrowable);
		aThrowable.printStackTrace();
	}

	@Override
	public void trace(String aMessage) {
		warn(aMessage);
	}

	@Override
	public void debug(String aMessage) {
		warn(aMessage);
	}

	@Override
	public void fatal(String aMessage) {
		error(aMessage);
	}

	@Override
	public void fatal(String aMessage, Throwable aThrowable) {
		error(aMessage, aThrowable);
	}

	// Print message in System.out.
	private void printOut(String aTag, String aMessage) {
		System.out.println(getPrintString(aTag, aMessage));
	}

	// Print message in System.out.
	private void printErr(String aTag, String aMessage) {
		System.err.println(getPrintString(aTag, aMessage));
	}

	// Get the string to print.
	private String getPrintString(String aTag, String aMessage) {
		return SDF.format(new Date()) + " [" + aTag + "]["
				+ className + "] " + aMessage;
	}

}
